<template>
  <div>
    <el-dialog
      :before-close="cancel"
      :title="info.isAdd ? '添加角色' : '编辑角色'"
      :visible.sync="info.isShow"
      center
    >
      <el-form ref="role" :model="roleinfo" :rules="rules">
        <el-form-item
          prop="rolename"
          label="角色名称"
          :label-width="formLabelWidth"
        >
          <el-input v-model="roleinfo.rolename" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          prop="rolename"
          label="角色权限"
          :label-width="formLabelWidth"
        >
          <el-tree
            ref="tree"
            :data="menulist"
            show-checkbox
            node-key="id"
            default-expand-all
            :props="{
              children: 'children',
              label: 'title',
            }"
            check-strictly
          >
          </el-tree>
        </el-form-item>

        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-switch
            v-model="roleinfo.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="2"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button @click="confirm" type="primary">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { roleAdd, roleInfo, roleEdit } from "../../request/api";
import { mapActions, mapGetters } from "vuex";

export default {
  data() {
    return {
      roleinfo: {
        rolename: "", //角色名称
        menus: "", //字符串 后端要求的是以','分割的字符串类型
        status: 1, //状态1正常2禁用
      },
      formLabelWidth: "120px",
      rules: {
        rolename: [
          { required: true, message: "请输入角色名称", trigger: "blur" },
          { min: 2, max: 8, message: "长度在 2 到 8 个字符", trigger: "blur" },
        ],
      },
    };
  },
  props: ["info"],
  methods: {
    ...mapActions({
      getMenu: "menu/getMenuAction",
      getRole: "role/getRoleAction",
    }),
    lookup(id) {//调用查询一条数据信息
      roleInfo({ id }).then((res) => {
        if (res.code == 200) {
          this.roleinfo = res.list;
          this.roleinfo.id = id;
          //给tree节点设置数据 返回的数据格式是字符串，我们要把它转化成数组
          this.$refs.tree.setCheckedKeys(this.roleinfo.menus.split(","));
        }
      });
    },
    cancel() {
      //关闭弹窗
      this.$emit("cancel", false);
      this.reset();
    },
    reset() {
      this.roleinfo = {
        rolename: "", //角色名称
        menus: "", //字符串 后端要求的是以','分割的字符串类型
        status: 1, //状态1正常2禁用
      };
    //   console.log(this.$refs.role.setCheckedKeys());
      this.$refs.role.resetFields(); //清空tree节点的选择状态
      this.$refs.tree.setCheckedKeys([]);
    },
    confirm() {
      //封装确定事件
      this.$refs.role.validate((val) => {
        if (val) {
          this.roleinfo.menus = this.$refs.tree.getCheckedKeys().join(",");
          if (this.info.isAdd) {
            roleAdd(this.roleinfo).then((res) => {
              if (res.code == 200) {
                this.$message.success(res.msg);
                this.cancel();
                this.getRole();
              } else {
                this.$message.error(res.msg);
              }
            });
          } else {
            roleEdit(this.roleinfo).then((res) => {
              if (res.code == 200) {
                this.$message.success(res.msg);
                this.cancel();
                this.getRole();
              } else {
                this.$message.error(res.msg);
              }
            });
          }
        } else {
          console.log("验证失败");
        }
      });
    },
  },
  computed: {
    ...mapGetters({
      menulist: "menu/getMenu",
    }),
  },
  mounted() {
    if (this.menulist.length == 0) {
      this.getMenu();
    }
  },
};
</script>

<style scoped>
</style>
